﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.SqlClient;
using System.Data;
using System.Configuration;
using WEB.Data;

namespace WEB.Utils
{
    public class DataAccess
    {
        //连接字符串
        //private string connStr = "Data Source=LYG-PC;User Id=sa;Pwd=admin;Initial Catalog=SMS;Integrated Security=True";
        private static string connStr = ConfigurationManager.ConnectionStrings["connString"].ConnectionString;
        // private string connStr = ConfigurationManager.AppSettings["connStr"].ToString();//ConfigReader.getConnstr();
        //可执行的SQL语句 
        private static string querySQL = "";
        private static SqlConnection conn = null;
        private static SqlCommand comm = null;

        public DataAccess()
        {
            init();
        }
        //初始化类的成员 
        private static void init()
        {
            conn = new SqlConnection();
            conn.ConnectionString = connStr;
            comm = conn.CreateCommand();
            conn.Open();
        }

        public static void setQuerySQL(string sql)
        {
            if (sql == "")
            {
                throw new Exception("字符串为空");
            }
            querySQL = sql;
            comm.CommandText = querySQL;
        }

        /// <summary>
        /// 查询数据库
        /// </summary>
        /// <param name="sql">查询语句</param>
        /// <returns>SqlDataReader  对象</returns>
        public SqlDataReader getReader(string sql)
        {
            setQuerySQL(sql);

            if (conn.State == System.Data.ConnectionState.Closed)
            {
                throw new Exception("数据库连接不成功！");
            }

            SqlDataReader reader = null;
            reader = comm.ExecuteReader();
            return reader;
        }
        /// <summary>
        /// 执行一个SQL语句，返回受影响的行数
        /// </summary>
        /// <param name="sql"></param>
        /// <param name="commParas"></param>
        /// <returns>受影响的行数</returns>
        public static int ExecuteNonQuery(string sql,params SqlParameter[] commParas)
        {

            //int val  = SqlHelper.ExecuteNonQuery(conn,CommandType.Text,sql,null);
            setQuerySQL(sql);
            PrepareCommand(comm, commParas);
            int val = comm.ExecuteNonQuery();
            comm.Parameters.Clear();
            return val;
        }

        private static void PrepareCommand(SqlCommand sqlCommand, params SqlParameter[] commParas)
        {
            if (commParas != null)
            {
                foreach (SqlParameter para in commParas)
                {
                    comm.Parameters.Add(para);
                }
            }
            throw new NotImplementedException();
        }

        /// <summary>
        /// 执行 SQL 语句 返回 DataSet
        /// </summary>
        /// <param name="sql">要执行的语句</param>
        /// <returns>返回 DataSet 对象</returns>
        public static DataSet nGetDataSet(string sql, params SqlParameter[] SqlParas)
        {
            setQuerySQL(sql);
            if (SqlParas != null)
            {
                foreach (SqlParameter para in SqlParas)
                {
                    comm.Parameters.Add(para);
                }
            }
            DataSet ds = new DataSet();
            SqlDataAdapter sda = new SqlDataAdapter();
            sda.SelectCommand = comm;
            sda.Fill(ds);
            comm.Dispose();
            conn.Close();
            conn.Dispose();
            sda.Dispose();
            return ds;
        }
        public static DataTable nGetDataTable(String sql,params SqlParameter[] commandParas)
        {
            DataSet ds = nGetDataSet(sql,commandParas);
            return ds.Tables[0];
        }
    }
}